package org.convallaria.system.biz.service;

import java.time.LocalDateTime;

/**
 * 安全审计服务接口
 * 
 * @author convallaria
 * @since 1.0.0
 */
public interface SecurityAuditService {

    /**
     * 记录安全事件
     * 
     * @param event 安全事件
     */
    void recordSecurityEvent(SecurityEvent event);

    /**
     * 记录登录事件
     * 
     * @param userId 用户ID
     * @param username 用户名
     * @param ip IP地址
     * @param success 是否成功
     * @param reason 失败原因
     */
    void recordLoginEvent(Long userId, String username, String ip, boolean success, String reason);

    /**
     * 记录权限变更事件
     * 
     * @param operatorId 操作人ID
     * @param targetUserId 目标用户ID
     * @param operation 操作类型
     * @param details 详细信息
     */
    void recordPermissionChangeEvent(Long operatorId, Long targetUserId, String operation, String details);

    /**
     * 记录敏感操作事件
     * 
     * @param userId 用户ID
     * @param operation 操作类型
     * @param resource 操作资源
     * @param details 详细信息
     */
    void recordSensitiveOperationEvent(Long userId, String operation, String resource, String details);

    /**
     * 记录密码变更事件
     * 
     * @param userId 用户ID
     * @param operatorId 操作人ID（自己修改为null）
     * @param success 是否成功
     * @param reason 失败原因
     */
    void recordPasswordChangeEvent(Long userId, Long operatorId, boolean success, String reason);

    /**
     * 安全事件
     */
    class SecurityEvent {
        private String eventId;
        private String eventType;
        private Long userId;
        private String username;
        private String ip;
        private String userAgent;
        private String operation;
        private String resource;
        private String details;
        private String result;
        private String reason;
        private LocalDateTime eventTime;
        private String tenantId;

        // 构造方法
        public SecurityEvent() {
            this.eventTime = LocalDateTime.now();
            this.eventId = java.util.UUID.randomUUID().toString();
        }

        // Builder模式
        public static Builder builder() {
            return new Builder();
        }

        public static class Builder {
            private SecurityEvent event = new SecurityEvent();

            public Builder eventType(String eventType) {
                event.eventType = eventType;
                return this;
            }

            public Builder userId(Long userId) {
                event.userId = userId;
                return this;
            }

            public Builder username(String username) {
                event.username = username;
                return this;
            }

            public Builder ip(String ip) {
                event.ip = ip;
                return this;
            }

            public Builder userAgent(String userAgent) {
                event.userAgent = userAgent;
                return this;
            }

            public Builder operation(String operation) {
                event.operation = operation;
                return this;
            }

            public Builder resource(String resource) {
                event.resource = resource;
                return this;
            }

            public Builder details(String details) {
                event.details = details;
                return this;
            }

            public Builder result(String result) {
                event.result = result;
                return this;
            }

            public Builder reason(String reason) {
                event.reason = reason;
                return this;
            }

            public Builder tenantId(String tenantId) {
                event.tenantId = tenantId;
                return this;
            }

            public SecurityEvent build() {
                return event;
            }
        }

        // Getters
        public String getEventId() { return eventId; }
        public String getEventType() { return eventType; }
        public Long getUserId() { return userId; }
        public String getUsername() { return username; }
        public String getIp() { return ip; }
        public String getUserAgent() { return userAgent; }
        public String getOperation() { return operation; }
        public String getResource() { return resource; }
        public String getDetails() { return details; }
        public String getResult() { return result; }
        public String getReason() { return reason; }
        public LocalDateTime getEventTime() { return eventTime; }
        public String getTenantId() { return tenantId; }
    }

    /**
     * 安全事件类型
     */
    enum SecurityEventType {
        LOGIN_SUCCESS("登录成功"),
        LOGIN_FAILURE("登录失败"),
        LOGOUT("登出"),
        PASSWORD_CHANGE("密码修改"),
        PASSWORD_RESET("密码重置"),
        PERMISSION_GRANT("权限授予"),
        PERMISSION_REVOKE("权限撤销"),
        ROLE_ASSIGN("角色分配"),
        ROLE_REVOKE("角色撤销"),
        ACCOUNT_LOCK("账户锁定"),
        ACCOUNT_UNLOCK("账户解锁"),
        SENSITIVE_OPERATION("敏感操作"),
        DATA_ACCESS("数据访问"),
        CONFIG_CHANGE("配置变更"),
        SECURITY_VIOLATION("安全违规");

        private final String description;

        SecurityEventType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
